package com.supermarket.demo1.controller;

import com.supermarket.demo1.mapper.UserMapper;
import com.supermarket.demo1.pojo.Result;
import com.supermarket.demo1.pojo.User;
import com.supermarket.demo1.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * 用户模块：
 *   登录
 *   注册
 *   。。
 * */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired // 从spring容器中获取到服务层接口对象。
    UserService userService;

   /* @RequestMapping("dologin")
    public String login(User user) {
        if (user == null) {
            return "index";
        } else if (user != null) {
            //从数据库查询该用户
            //能够获取到用户输入

            String input_userCode = user.getUserCode();
            String input_userPassword = user.getUserPassword();
            System.out.println("获取用户输入的用户名 "+input_userCode+"密码: "+input_userPassword);

            User db_user = userMapper.selectUserByName(user.getUserCode());
            String db_userName = db_user.getUserCode();
            String db_userPwd = db_user.getUserPassword();
            System.out.println("数据库查询的用户名: "+db_userName+"密码: "+db_userPwd);
            if(input_userCode.equals(db_userName)&& input_userPassword.equals(db_userPwd)){
                System.out.println("登录成功");
                return "frame";
            }else {
                System.out.println("登录失败");
                return "index";
            }
        }
    return "None";

    }*/

    /**
     * @param user
     * @return
     */
    @RequestMapping("/dologin") //
    public String login(User user, Model model, HttpSession session) {
        User login = userService.login(user);
        System.out.println(user);
        if (login == null) {
            //登录失败，提示用户重新登录 error
            model.addAttribute("error", "用户名或密码错误,请重新输入!");

            System.out.println("登录失败");
            return "login";
        }
        //登录成功 保存到session ，微服务  单点登录系统
        //如何获取session对象。
        //1. 可以直接从前端页面传入 userSession 与前端页面保持一致
        session.setAttribute("userSession", login);
        return "frame";
    }

    /**
     * 用户管理  显示用户列表信息
     *
     * @return
     */
    @RequestMapping("userlist")
    public String userList(Model model) {
        List<User> userList = userService.getALlUser();

        model.addAttribute("userList", userList);

        return "userlist";
    }

    //退出登录
    @RequestMapping("exit")
    public String exit(HttpSession session) {
        //销毁session
//        session.invalidate();
        session.removeAttribute("userSession");
        return "login";
    }

    /**
     * 根据传入的用户id删除用户
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    @ResponseBody
    public Result deleteUser(@PathVariable int id) {
        int count = userService.deleteUser(id);
        if (count == 0) {
            System.out.println("登录失败");
            return new Result(400, "fail");
        }
        return new Result(200, "success");
    }

}
